/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plagiadetector.utility;

import java.io.File;
import java.util.ArrayList;

/**
 * Utility for handling files.
 * @author Dell
 */
public class FileUtils {
    
   private static FileUtils mainutils;
   
   private FileUtils(){
   
   }
   
   private static void initiate(){
       if(mainutils == null){
           mainutils = new FileUtils();
       }
   }
   
   public static FileUtils getInstance(){
       initiate();
       return mainutils;
   }
   /**
    *  Get filenames in a directory with a certain type. 
    * @param addr Directory address.
    * @param filetype File type that going to be searched. example: "xml", "*.xml", "txt", "*.txt". Passing "" or "*.*" will detect all file.
    * @return ArrayList<String> of filenames (not including sub-folders).
    */
   public static ArrayList<String> getFileNames(String addr, String filetype){
       ArrayList<String> filenames = new ArrayList<String>();
       try{
           File f = new File(addr);
           
           if(f.isDirectory()){
               File fs[] = f.listFiles();
               
               for(int i=0; i<fs.length; i++){
                   if(!fs[i].isDirectory()){
                       String filename = fs[i].getName();
                       filetype = filetype.trim();
                       filetype = filetype.replace("*", "");
                       filetype = filetype.replace(".", "");
                       
                       if(filename.endsWith(filetype)){
                           filenames.add(filename);
                       }
                   }
               }
             fs = null; //release
           }   
           f = null; //release  
       }catch(Exception e){
           e.printStackTrace();
       }
       return filenames;
   }
   
   /**
    * Get all filenames in a directory.
    * @param addr Directory address.
    * @return ArrayList<String> of filenames (not including sub-folders).
    */
   public static ArrayList<String> getFileNames(String addr){
       return getFileNames(addr, "");
   }
  
   /**
   *  Get all sub-directories from a directory.
   * @param addr Directory address.
   * @return ArrayList<String> of sub-directories.
   */
   public static ArrayList<String> getSubDirectories(String addr){
       ArrayList<String> subdirs = new ArrayList<String>();
       try{
           File f = new File(addr);
           
           if(f.isDirectory()){
               File fs[] = f.listFiles();
               
               for(int i=0; i<fs.length; i++){
                   if(fs[i].isDirectory()){
                       subdirs.add(fs[i].getName());
                   }
               }
             fs = null; //release
           }   
           f = null; //release  
       }catch(Exception e){
           e.printStackTrace();
       }           
       return subdirs;
   }
}
